package cn.itcast.web.controller.basics;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.itcast.domain.cargo.Factory;
import cn.itcast.domain.cargo.FactoryExample;
import cn.itcast.domain.cargo.Product;
import cn.itcast.domain.cargo.ProductExample;
import cn.itcast.service.cargo.FactoryService;
import cn.itcast.service.cargo.ProductService;
import cn.itcast.util.ImageUploadUtils;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping(value = "/basics/product",name = "产品管理")
public class ProductController extends BaseController {

    //依赖注入
    @Reference
    private ProductService productService;

    @Reference
    private FactoryService factoryService;

    //分页查询
    @RequestMapping(value = "/list",name = "分页查询")
    public String list(@RequestParam(defaultValue = "1")Integer pageNum, @RequestParam(defaultValue = "5")Integer pageSize, ProductExample example){

        //创建
        example.setOrderByClause("create_time desc");

        //调用方法
        PageInfo page = productService.findByPage(pageNum, pageSize, example);

        //存入域中
        request.setAttribute("page",page);

        //创建厂家查询对象
        FactoryExample factoryExample = new FactoryExample();

        //查询厂家列表
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        //存入域中
        request.setAttribute("factoryList",factoryList);

        //跳转页面
        return "system/product/product-list";

    }

    //跳转新增页面
    @RequestMapping(value = "/toAdd",name = "跳转新增页面")
    public String toAdd(){

        //创建厂家查询对象
        FactoryExample factoryExample = new FactoryExample();

        //查询厂家列表
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        //存入域中
        request.setAttribute("factoryList",factoryList);


        //跳转页面
        return "system/product/product-add";
    }


    //新增修改方法
    @RequestMapping(value = "/edit",name = "新增/修改")
    public String edit(Product product, MultipartFile productPhoto ) throws IOException {

        //非空判断
        if (productPhoto!=null && !productPhoto.isEmpty()){
            //调用七牛云
            String imgUrl = ImageUploadUtils.upload(productPhoto.getInputStream());
            product.setProductImage(imgUrl);
        }

        //判断是否为空
        if (StrUtil.hasEmpty(product.getId())){
            //新增
            //设置id
            product.setId(IdUtil.simpleUUID());

            //打印
            //System.out.println("========="+product);

            //调用新增方法
            productService.save(product);

        }else{
            //修改方法
            productService.update(product);
        }

        //重定向
        return "redirect:/basics/product/list.do";
    }


    //跳转修改页面
    @RequestMapping(value = "/toUpdate",name = "跳转新增页面")
    public String toUpdate(String id){

        //获取厂家信息
        FactoryExample factory = new FactoryExample();
        //调用方法
        List<Factory> list = factoryService.findAll(factory);
        //存入域中
        request.setAttribute("factoryList",list);


        //调用方法
        Product page = productService.findById(id);
        //存入域中
        request.setAttribute("page",page);
        //跳转页面
        return "system/product/product-update";
    }

    //删除
    @RequestMapping(value = "/delete",name = "删除")
    public String delete(String id){
        //非空判断
        if (id !=null){
            //调用方法
            productService.delete(id);
        }

        //重定向页面
        return "redirect:/basics/product/list.do";
    }

}
